<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
use \Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
use \Aliyun\Core\Profile\DefaultProfile;
use \Aliyun\Core\Regions\EndpointConfig;
use \Aliyun\Core\DefaultAcsClient;
// 应用公共文件
//发送短信
function sms($phone,$sina,$code,$data){
    // return true;

    require_once ROOT_PATH."extend/sms/api_sdk//vendor/autoload.php";//引入PHPExcel加载文件
    // 初始化SendSmsRequest实例用于设置发送短信的参数
    $request = new SendSmsRequest();

    //可选-启用https协议
    //$request->setProtocol("https");
    // 必填，设置短信接收号码
    $request->setPhoneNumbers($phone);
    // 必填，设置签名名称，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
    $request->setSignName($sina);
    // 必填，设置模板CODE，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
    $request->setTemplateCode($code);
    // 可选，设置模板参数, 假如模板中存在变量需要替换则为必填项

    $request->setTemplateParam($data);

    // 可选，设置流水号
    $request->setOutId("yourOutId");
    // 选填，上行短信扩展码（扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段）
    $request->setSmsUpExtendCode("1234567");
    // 发起访问请求
    $acsResponse = getAcsClient()->getAcsResponse($request);

    return $acsResponse;
}


function isPoneLandline($phone){
    $isMob="/^1[3456789]{1}\d{9}$/";
    $isTel="/^([0-9]{3,4}-)?[0-9]{7,8}$/";
    if(!preg_match($isMob,$phone) && !preg_match($isTel,$phone)){
        return false;
    }else{
        return true;
    }
}

//记录日志
function writeRuntimeLog($file='logs',$json=[]){
    file_put_contents(LOG_PATH . $file . '_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').':'.json_encode($json,JSON_UNESCAPED_UNICODE)."\r\n",FILE_APPEND);
}

//成功数据返回
function buildSuccess($data = [],$msg = "操作成功",$code = 0){
    return json_encode(["code" => $code ,"data" => $data , "msg" => $msg],JSON_UNESCAPED_UNICODE);
}

//失败数据返回
function buildError($msg = "操作失败",$code = 1,$data = []){
    return json_encode(["code" => $code ,"data" => $data , "msg" => $msg],JSON_UNESCAPED_UNICODE);
}

//失败数据返回
function hearError($msg = "操作失败",$code = 1,$data = []){
    return json(["code" => $code ,"data" => $data , "msg" => $msg]);
}

function getAcsClient(){
    //产品名称:云通信短信服务API产品,开发者无需替换
    $product = "Dysmsapi";
    //产品域名,开发者无需替换
    $domain = "dysmsapi.aliyuncs.com";
    $accessKeyId = "LTAI4Fz9xJYZjEzu9ebURG3k"; // AccessKeyId
    $accessKeySecret = "Iyhgi4NswtUDKRs1UGOqvm67Jbfu0f"; // AccessKeySecret
    // 暂时不支持多Region
    $region = "cn-hangzhou";
    // 服务结点
    $endPointName = "cn-hangzhou";

    //初始化acsClient,暂不支持region化
    $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
    // 手动加载endpoint
    EndpointConfig::load();
    // 增加服务结点
    DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);
    // 初始化AcsClient用于发起请求
     $acsClient = new DefaultAcsClient($profile);
     return $acsClient;
}


if (!function_exists("curlPost")) {
    /*
 * curl请求
 * @param url string 请求地址
 * @param post_data 数据
 */
    function curlPost($url, $post_data = [], $header = [])
    {
        if (is_array($post_data)) {
            $post_string = http_build_query($post_data, '', '&');
        } else {
            $post_string = json_encode($post_data);
        }
        $ch = curl_init();    // 启动一个CURL会话
        curl_setopt($ch, CURLOPT_URL, $url);     // 要访问的地址

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  // 对认证证书来源的检查   // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的Post请求
        if ($header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);     // Post提交的数据包
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);     // 设置超时限制防止死循环
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     // 获取的信息以文件流的形式返回
        $result = curl_exec($ch);
        curl_close($ch);
//    writeRuntimeLog("Monitor",$result);
        return json_decode($result, true);
    }
}

?>